#include <iostream>
#include <string>
#include <functional>
#include <time.h>
#include <unistd.h>
#include <pthread.h>

// typedef std::function<void()> func_t; 下面的写法和这句代码等价
using func_t = std::function<void()>;   // C++11新语法

// 线程的相关数据
class ThreadData
{
public:
    ThreadData(const std::string &name, const uint64_t &ctime, func_t f)
        :threadname(name), createtime(ctime), func(f)
    {}

public:
    std::string threadname; // 线程名
    uint64_t createtime;    // 线程创建时间
    func_t func;            // 该线程执行的方法
};

void Print()
{
    std::cout << "我是线程执行的大任务的一部分" << std::endl;
}

// 新线程
void *ThreadRountine(void *args)
{
    ThreadData *td = static_cast<ThreadData*>(args);
    while(true)
    {
        std::cout << "new thread" << std::endl;
        std::cout << "thread name: " << td->threadname << std::endl;
        std::cout << "create time: " << td->createtime << std::endl;
        td->func();
        sleep(3);
    }
}

// 主线程
int main()
{
    pthread_t tid;
    ThreadData *td = new ThreadData("thread 1", (uint64_t)(time(nullptr)), Print);
    // 传参直接传td
    pthread_create(&tid, nullptr, ThreadRountine, td);

    while(true)
    {
        std::cout << "main thread" << std::endl;
        sleep(1);
    }
}